import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, Query } from '@nestjs/common';
import { FriendService } from './friend.service';
import { CreateFriendDto } from './dto/create-friend.dto';
import { UpdateFriendDto } from './dto/update-friend.dto';
import { returnFail, returnSuccess } from 'src/common/response';
import { CODE, TRes } from 'src/common/base';
import { User } from '@prisma/client';
import { AuthGuard } from '@nestjs/passport';

@UseGuards(AuthGuard('jwt'))
@Controller('/api/friend')
export class FriendController {
  constructor(private readonly friendService: FriendService) {}

  // 查询注册的用户并排除已成为好友的数据，用于添加好友
  @Get('/friendList')
  async queryNotFriendsList(@Query() req: { username: string, userId: string }) {
    if (!req.username) {
      return returnFail(CODE.PARAMERROR, 'username 不能为空')
    }
    const user: TRes<User[]>  = await this.friendService.queryNotFriendsList(req.username, req.userId)
    if (user.sql) {
      return returnFail(CODE.SQLERROR, user.msg)
    }
    return returnSuccess(user.msg, user.data)
  }


  @Get('/friendListByUserId')
  async queryFriendsListByUserId(@Query() req: { userId: string }) {
    if (!req.userId) {
      return returnFail(CODE.PARAMERROR, 'userId 不能为空')
    }
    const user: TRes<User[]>  = await this.friendService.queryFriendList(req.userId)
    if (user.sql) {
      return returnFail(CODE.SQLERROR, user.msg)
    }
    return returnSuccess(user.msg, user.data)
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateFriendDto: UpdateFriendDto) {
    return this.friendService.update(+id, updateFriendDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.friendService.remove(+id);
  }
}
